# _*_ coding:utf-8 _*_
# @Time  : 2023.04.10
# @Author: zizlee


import PyPDF2
import os


def get_files():
    pdf_files = []
    for filename in os.listdir('files'):
        if filename.endswith('.pdf'):
            pdf_files.append(f'files/{filename}')
            pdf_files.sort(key=str.lower)
    pdf_files.sort()
    return pdf_files


def combine(file_list):
    pdf_writer = PyPDF2.PdfWriter()
    for filename in file_list:
        print(filename)
        pdf = open(filename, 'rb')
        pdf_reader = PyPDF2.PdfReader(pdf)
        for page in pdf_reader.pages:
            pdf_writer.add_page(page)

    out_put = open('王翠冰合同.pdf', 'wb')
    pdf_writer.write(out_put)
    out_put.close()


if __name__ == '__main__':
    combine(get_files())
